<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/3/8
 * Time: 17:58
 */

namespace Admin\Controller;


use Think\Controller;
use Think\Upload;
use Qiniu\Auth;

class StoreController extends Controller
{
    /*
     * 商家创建店铺
     * @wxj
     * */
    public function createShop()
    {
        $types = M("type_info");
        $infos = $types->select();
        $this->assign("infos",$infos);
        $this->display('addShop');
    }

    /*
     * 店铺菜单控制器
     * 如果没有店铺，创建店铺，如果有店铺展示店铺列表
     * @wxj
     * */
    public function selectShop()
    {
        $uid = session('uid');
        $shop = M("shop_info");
        $datas = $shop->where('uid=' . $uid)->select();
        if (empty($datas)) {
            $this->display("createShop");
        } else {
            redirect("shopList");
        }

    }

    /*
     * 创建店铺表单接受处理
     * @wxj
     * */
    public function createPost()
    {
        $data = I('post.');
        $uid = session('uid');
        $shop = M("shop_info");
//        追加一个元素
        $creatTime = date('Ymd-His', NOW_TIME);
        $imgTop = $data['name'] . 'top-' . $creatTime;
        $data['uid'] = $uid;
        $data['updated'] = NOW_TIME;
        $type = $data['type'];
        $typeStr = implode(",", $type);
        $data['type'] = $typeStr;

//            处理上传文件
        $upload = new Upload();
        $upload->maxSize = 5145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/shopImg/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->saveName = $imgTop;
//            $upload->subName = '';$imgTop;
        $upload->subName = '';
// 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功 获取上传文件信息
            foreach ($info as $file) {
//                    echo $file['savepath'] . $file['savename'];
                $data['img'] = $file['savename'];
                //缩略图
                $image = new \Think\Image();
                $image->open("./Public/shopImg/" . $data['img']);
                $smallImgTop = $data['name'] . 'small-' . $creatTime.".";
                $midImgTop = $data['name'] . 'middle-' . $creatTime.".";
                $ext = pathinfo($data['img'], PATHINFO_EXTENSION);
                $image->thumb(150, 150)->save("./Public/shopImg/" . $smallImgTop . $ext);
                $image->thumb(500, 300)->save("./Public/shopImg/" . $midImgTop . $ext);
                $data["sm_img"] = $smallImgTop . $ext;
                $data["mid_img"] = $midImgTop . $ext;

                $shopArr = $data;
                $shop->add($shopArr);
                redirect("shopList");
            }
        }
    }

    /*
     * 商家店铺列表
     * ＠ｗｘｊ
     * */
    public function shopList()
    {
        $uid = session('uid');
        $shop = M("shop_info");
        $shopImg = M("shop_img");
        $datas = $shop->where('uid=' . $uid)->select();
        $imgs = $shopImg->where('uid=' . $uid)->select();
        if (!empty($imgs)) {
            $this->assign("statu", 1);
        }
        $this->assign("datas", $datas);
        $this->assign("imgs", $imgs);
        $this->display();
    }

    /*
     * 商家编辑店铺信息
     * @wxj
     * */
    public function editShop()
    {
        $uid = session('uid');
        $shop = M("shop_info");
        $types = M("type_info");

        $infos = $types->select();
        $datas = $shop->where('uid=' . $uid)->find();
        $this->assign("datas", $datas);
        $this->assign("infos",$infos);
        $this->display();
    }

    /*
     * 商家编辑表单提交
     * @wxj
     * */
    public function editPost()
    {
        $data = I('post.');
        $uid = session('uid');
        $shop = M("shop_info");
        $type = $data['type'];
        $typeStr = implode(",", $type);
        $data['type'] = $typeStr;
        $res = $shop->where("uid=" . $uid)->save($data);
        if ($res == false) {//更新失败
            $this->error('更新失败');
        }
        redirect("shopList");
    }

    /*
     * 发布推荐（多张图片上传）
     * @wxj
     * */
    public function pushRecommend()
    {
        $this->display();
    }

    /*
     * 处理多图片上传的
     * @wxj
     * */
    public function recommendPost()
    {
//        $postdata = I('post.');
        $uid = session('uid');
        $shop = M("shop_info");
        $shopInfo = $shop->where("uid=" . $uid)->find();
        $shopImg = M("shop_img");
        $imgDatas = $shopImg->where("uid=" . $uid)->find();
//        追加一个元素
        $creatTime = date('Ymd-His', NOW_TIME);
        $imgTop = $shopInfo['name'] . 'Recom-' . $creatTime;
        $data['uid'] = $uid;
        $data['shop_id'] = $shopInfo['shop_id'];
        $data['updated'] = NOW_TIME;

//            处理上传文件
        $upload = new Upload();
        $upload->maxSize = 5145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/shopImg/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->saveName = array('uniqid', $imgTop);
//            $upload->subName = '';$imgTop;
        $upload->subName = '';
// 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功 获取上传文件信息
            if (!empty($imgDatas)) {
                $shopImg->where("uid=" . $uid)->delete();
            }
            foreach ($info as $file) {
//                    echo $file['savepath'] . $file['savename'];
                $data['img'] = $file['savename'];
                $shopArr = $data;
                $shopImg->add($shopArr);
            }
            redirect("shopList");
        }
    }

    /*
     * 商家联系我们
     * @wxj
     * */
    public function contactUs()
    {

        $this->display();
    }

    /*
     * 商家联系我们表单提交
     * @wxj
     * */
    public function contactPost()
    {
        if (!IS_POST) {
            $this->error("非法请求");
        }
        $data = I("post.");
        $uid = session('uid');
        $sug = M("user_sug");
        $data['uid'] = $uid;
        $data['updated'] = time();
        $res = $sug->add($data);
        if (!$res) {
            $this->error("提交失败，请重新提交");
        }
        $this->assign('desc', "提交成功");
        $this->assign("status", 200);
        $this->display("contactUs");

    }

    /*
     * 商家商品
     * ＠wan
     * */
    public function goodsList($id)
    {
        $uid = session("uid");
        if ($uid == null || $uid == '') {
            if (isMobile()) {
                $this->display("User/index_bf");
                return;
            } else {
                $this->display("User/index");
                return;
            }
        }
        $goods = M("shop_goods");
        $res = $goods->where("shop_id=" . $id)->select();
        if(empty($res)){
            $this->assign("code", 4002);
            $this->assign("shop_id", $id);
            $this->assign("desc", "您还没有商品，赶紧添加吧");
        }else{
            $this->assign("datas", $res);
            $this->assign("shop_id", $id);
        }
        $this->display();
    }
    /*
     * 商家添加商品页面
     * ＠wan
     * */
    public function addGoods($id){
        vendor('qiniu.autoload');
        // 用于签名的公钥和私钥
        $accessKey = 'KN1B6wd3Gl1cRKpywQExkr1bh9FSAIPCzizIq77R';
        $secretKey = 'JkN4e_sfUxqwna1mXgZ4XIiiJvo-_VIN2HNWjGuZ';
        // 初始化签权对象
        $auth = new Auth($accessKey, $secretKey);
        $bucket = 'ding';
        $upToken = $auth->uploadToken($bucket);
        $this->assign("shop_id",$id);
        $this->assign("upToken",$upToken);
        $this->display();
    }
    /*
     * 商家添加商品ｐｏｓｔ
     * @wan
     *
     * */
    public function addGoodsPost(){
        $data = I("post.");
        $goods = M("shop_goods");
        $data['updated'] = NOW_TIME;

        $creatTime = date('Ymd-His', NOW_TIME);
        $imgTop = $data['name'] . 'goods-' . $creatTime;
        //处理上传文件
        $upload = new Upload();
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/goodsImg/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->saveName = $imgTop;
        $upload->subName = '';
// 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功 获取上传文件信息
            foreach ($info as $file) {
                $data['img'] = $file['savename'];
                //缩略图
                $image = new \Think\Image();
                $image->open("./Public/goodsImg/" . $data['img']);
                $smallImgTop = $data['name'] . 'small-' . $creatTime. ".";
                $ext = pathinfo($data['img'], PATHINFO_EXTENSION);
                $image->thumb(200, 200)->save("./Public/goodsImg/" . $smallImgTop .$ext);
                $data["sm_img"] = $smallImgTop . $ext;
                $goods->add($data);
                redirect("goodsList/id/".$data['shop_id']);
            }
        }
    }
    /*
     * 七牛php
     * ＠wan
     * */
    public function upToken(){
        vendor('qiniu.autoload');
        // 用于签名的公钥和私钥
        $accessKey = 'KN1B6wd3Gl1cRKpywQExkr1bh9FSAIPCzizIq77R';
        $secretKey = 'JkN4e_sfUxqwna1mXgZ4XIiiJvo-_VIN2HNWjGuZ';
        // 初始化签权对象
        $auth = new Auth($accessKey, $secretKey);
        $bucket = 'http://7xt91r.com1.z0.glb.clouddn.com/';
        $token = $auth->uploadToken($bucket);
        $data['token'] = $token;
        $this->ajaxReturn($data);
    }
    /*test*/
    public function test(){
        vendor('qiniu.autoload');
        // 用于签名的公钥和私钥
        $accessKey = 'KN1B6wd3Gl1cRKpywQExkr1bh9FSAIPCzizIq77R';
        $secretKey = 'JkN4e_sfUxqwna1mXgZ4XIiiJvo-_VIN2HNWjGuZ';
        // 初始化签权对象
        $auth = new Auth($accessKey, $secretKey);
        $bucket = 'dici';
        $upToken = $auth->uploadToken($bucket);
        $this->assign("upToken",$upToken);
        $this->display();
    }
}